package net.a6polo.a6poloweibo;

import java.io.IOException;

import com.google.gson.Gson;
import com.weibo.sdk.android.Oauth2AccessToken;
import com.weibo.sdk.android.WeiboException;
import com.weibo.sdk.android.api.CommentsAPI;
import com.weibo.sdk.android.api.WeiboAPI;
import com.weibo.sdk.android.net.RequestListener;

import net.a6polo.a6poloweibo.adapter.CommentListViewAdapter;
import net.a6polo.a6poloweibo.sina.domain.Comments;
import net.a6polo.a6poloweibo.view.DropDownToRefreshListView;
import net.a6polo.a6poloweibo.view.MyProgressDialog;
import net.a6polo.a6poloweibo.view.DropDownToRefreshListView.OnRefreshListener;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class CommentActivity extends Activity {

	private DropDownToRefreshListView commentsListView = null;

	private CommentListViewAdapter commentListViewAdapter;

	private Comments comments;

	private static String weiboId;

	private Exception exception;

	private Handler handler = null;

	private CommentsAPI commentApi;

	private int pageNo = 1;

	private int pageSize = 20;

	private long maxId = 0;

	private Dialog progressDialog;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_comment);

		handler = new Handler();
		commentsListView = (DropDownToRefreshListView) findViewById(R.id.commentsListView);
		progressDialog = new MyProgressDialog(this, R.style.CustomProgressDialog);

		Intent intent = getIntent();
		weiboId = intent.getStringExtra("weiboId");
		Context ctx = CommentActivity.this;
		SharedPreferences sp = ctx.getSharedPreferences("token", MODE_PRIVATE);
		String token = sp.getString("access_token", "none");
		String expires_in = sp.getString("expires_in", "none");
		Oauth2AccessToken accessToken = new Oauth2AccessToken(token, expires_in);

		commentApi = new CommentsAPI(accessToken);

		if (null == comments || comments.getComments().size() == 0) {
			progressDialog.show();
			reRefresh();
		}

		commentsListView.setOnRefreshListener(new OnRefreshListener() {
			@Override
			public void onRefresh() {
				reRefresh();
			}
		});

		commentsListView.setOnLoadMoreListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				more();
			}
		});
	}

	class WeiboCommentRequestListener implements RequestListener {

		@Override
		public void onComplete(String json) {
			progressDialog.dismiss();
			Gson gson = new Gson();
			comments = gson.fromJson(json, Comments.class);

			int maxIndex = comments.getComments().size();
			if (maxIndex > 0) {
				maxIndex = maxIndex - 1;
				maxId = comments.getComments().get(maxIndex).getId();
			}

			new Thread() {
				public void run() {
					handler.post(runnableCommentUi);
				}
			}.start();

		}

		@Override
		public void onError(WeiboException e) {
			exception = e;
			runOnUiThread(new Runnable() {
				@Override
				public void run() {
					Toast.makeText(getApplicationContext(), " error : " + exception.getMessage(), Toast.LENGTH_LONG)
							.show();
				}
			});

		}

		@Override
		public void onIOException(IOException e) {
			exception = e;
			runOnUiThread(new Runnable() {
				@Override
				public void run() {
					Toast.makeText(getApplicationContext(), " error : " + exception.getMessage(), Toast.LENGTH_LONG)
							.show();
				}
			});

		}

	}

	Runnable runnableCommentUi = new Runnable() {

		@Override
		public void run() {

			loadCommentData(comments);
		}

	};

	private void loadCommentData(Comments comments) {
		commentListViewAdapter = new CommentListViewAdapter(comments, this);
		commentsListView.setAdapter(commentListViewAdapter);
	}

	private void moreCommentData(Comments comments) {
		commentListViewAdapter.addItem(comments);
		commentListViewAdapter.notifyDataSetChanged();
	}

	private void reRefresh() {
		maxId = 0;
		commentApi.show(Long.parseLong(weiboId), (long) 0, (long) maxId, pageSize, pageNo, WeiboAPI.AUTHOR_FILTER.ALL,
				new WeiboCommentRequestListener());
	}

	private void more() {
		commentApi.show(Long.parseLong(weiboId), (long) 0, (long) maxId, pageSize, pageNo, WeiboAPI.AUTHOR_FILTER.ALL,
				new WeiboCommentMoreRequestListener());
	}

	class WeiboCommentMoreRequestListener implements RequestListener {

		@Override
		public void onComplete(String json) {
			Gson gson = new Gson();
			comments = gson.fromJson(json, Comments.class);

			int maxIndex = comments.getComments().size();
			if (maxIndex > 0) {
				maxIndex = maxIndex - 1;
			}
			maxId = comments.getComments().get(maxIndex).getId();

			new Thread() {
				public void run() {
					handler.post(runnableMoreCommentUi);
				}
			}.start();

		}

		@Override
		public void onError(WeiboException e) {
			exception = e;
			runOnUiThread(new Runnable() {
				@Override
				public void run() {
					Toast.makeText(getApplicationContext(), " error : " + exception.getMessage(), Toast.LENGTH_LONG)
							.show();
				}
			});

		}

		@Override
		public void onIOException(IOException e) {
			exception = e;
			runOnUiThread(new Runnable() {
				@Override
				public void run() {
					Toast.makeText(getApplicationContext(), " error : " + exception.getMessage(), Toast.LENGTH_LONG)
							.show();
				}
			});

		}

	}

	Runnable runnableMoreCommentUi = new Runnable() {

		@Override
		public void run() {
			moreCommentData(comments);
		}

	};
}
